package com.iamteer.contact.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iamteer.contact.entity.Contact;
import com.iamteer.contact.service.impl.ContactServiceImpl;

/**
 * Servlet implementation class QueryContact
 */
@WebServlet("/QueryContact")
public class QueryContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryContact() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		//接收 ID
		String id = request.getParameter("id");	//contact id
		//查询对应联系人
		ContactServiceImpl service = new ContactServiceImpl();
		Contact contact = service.findById(id);
		if (contact == null) {
			response.sendRedirect(getServletContext().getContextPath() + "/ListContact");
			return;
		}
		//保存到 request 域中
		request.setAttribute("contact", contact);
		//转发到修改联系人页面
		request.getRequestDispatcher("/updateContact.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
